package com.prs.crm.action.project.report;

import java.util.Collection;
import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.project.Project;
import com.prs.crm.domain.project.Task;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.service.project.ProjectService;
import com.prs.crm.service.project.TaskService;

public class ProjectReportAction extends ReportAction {

	private static final long serialVersionUID = -2108961553966242623L;

	private ProjectService projectService;
	private TaskService taskService;
	private ChartResult chartResult;
	private ChartService chartService;
	private PaginationSupport pagination;

	private int state;
	private Date datestart;
	private Date dateend;
	private String acceptnames;
	private String accepts;
	private Project project;

	@Action("editProjectPie")
	public String editProjectPie() {
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	@Action(value = "viewProjectPie", results = {
			@Result(name = "success", type = "chain", location = "editProjectPie"),
			@Result(name = "error", type = "chain", location = "editProjectPie") })
	public String viewProjectPie() {
		if (datestart == null) {
			addActionError("开始时间不能为空");
			return ERROR;
		}
		if (dateend == null) {
			addActionError("结束时间不能为空");
			return ERROR;
		}
		if (accepts == null || "".equals(accepts.trim())) {
			addActionError("执行人不能为空");
			return ERROR;
		}
		Collection<?> dataList = projectService.getProjectReportDataSource(
				state, datestart, dateend,
				!accepts.trim().equals("") ? chartService
						.convertIdsToPersons(ToolKits
								.convertStringAryToIntAry(accepts.split(",")))
						: null);
		chartResult = chartService.export(chartService.fillDataset(
				(Collection<Object[]>) dataList, Constrains.REPORT_PIECHART),
				"项目分析", "#");
		return SUCCESS;
	}

	@Action("editProjectReport")
	public String editProjectReport() {
		return SUCCESS;
	}

	@Action("listProjectReport")
	public String listProjectReport() {
		pagination = this.getPaginationHelper().prepare(pagination);
		DetachedCriteria criteria = this.getProjectService().createQuery(
				Project.class);
		if (datestart != null) {
			criteria.add(Restrictions.ge("startTime", datestart));
		}
		if (dateend != null) {
			criteria.add(Restrictions.le("startTime", dateend));
		}
		if (accepts != null && !accepts.trim().equals("")) {
			criteria.add(Restrictions.in("recorder", chartService
					.convertIdsToPersons(ToolKits
							.convertStringAryToIntAry(accepts.split(",")))));
		}
		if (state != 0) {
			criteria.add(Restrictions.eq("status", state));
		}
		this.setPagination(this.getProjectService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "exportProjectReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportProjectReport() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Project.class);
		ReportTable table = new ReportTable("项目报表");
		table.addCell(new ReportCell("项目编号", "id"));
		table.addCell(new ReportCell("项目名称", "name"));
		table.addCell(new ReportCell("制定人", "recorder.resume.name"));
		table.addCell(new ReportCell("项目类型", "type.name"));
		table.addCell(new ReportCell("项目来源", "source.name"));
		table.addCell(new ReportCell("项目阶段", "stage.name"));
		table.addCell(new ReportCell("项目地区", "district.name"));
		table.addCell(new ReportCell("项目预算", "budget"));
		table.addCell(new ReportCell("起始时间", "startTime"));
		table.addCell(new ReportCell("计划结束时间", "endTime"));
		table.addCell(new ReportCell("项目状态", "flowstatus", Constrains
				.getReportProjectTaskStatus()));
		table.addCell(new ReportCell("状态", "status", Constrains
				.getReportProjectStatus()));

		if (datestart != null) {
			criteria.add(Restrictions.ge("startTime", datestart));
		}
		if (dateend != null) {
			criteria.add(Restrictions.le("startTime", dateend));
		}
		if (accepts != null && !accepts.trim().equals("")) {
			criteria.add(Restrictions.in("recorder", chartService
					.convertIdsToPersons(ToolKits
							.convertStringAryToIntAry(accepts.split(",")))));
		}
		if (state != 0) {
			criteria.add(Restrictions.eq("status", state));
		}
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}

	@Action("editProjectTaskReport")
	public String editProjectTaskReport() {
		pagination = this.getPaginationHelper().prepare(pagination);
		DetachedCriteria criteria = this.getProjectService().createQuery(
				Project.class);
		this.setPagination(this.getProjectService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listProjectTaskReport")
	public String listProjectTaskReport() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Task.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("project", project));
		criteria.add(Restrictions.eq("status", Constrains.TASK_STATUS_PROCESS))
				.add(
						Restrictions.ne("flowstatus",
								Constrains.WORKFLOWENTITY_STATE_INVALID));
		pagination.setOrders(new Order[] { Order.desc("id") });
		this.setPagination(this.getProjectService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action(value = "exportProjectTaskReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportProjectTaskReport() {
		try {
			DetachedCriteria criteria = this.getPaginationHelper().createQuery(
					Task.class);
			ReportTable table = new ReportTable("项目任务报表");
			table.addCell(new ReportCell("任务编号", "id"));
			table.addCell(new ReportCell("任务名称", "name"));
			table.addCell(new ReportCell("下达人", "recorder.resume.name"));
			table.addCell(new ReportCell("任务级别", "grade", new Constrains()
					.getTaskGrades()));
			table.addCell(new ReportCell("起始时间", "startTime"));
			table.addCell(new ReportCell("计划结束时间", "endTime"));
			table.addCell(new ReportCell("任务进度", "rate", '%'));
			table.addCell(new ReportCell("审核状态", "flowstatus", Constrains
					.getReportProjectTaskStatus()));

			criteria.add(Restrictions.eq("project", project)).add(
					Restrictions.eq("status", Constrains.TASK_STATUS_PROCESS))
					.add(
							Restrictions.ne("flowstatus",
									Constrains.WORKFLOWENTITY_STATE_INVALID));
			criteria.addOrder(Order.desc("id"));
			this.setOutFilePath(this.getReportService().report(criteria, table,
					new ExportConfig(), this.getExportType()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public Date getDatestart() {
		return datestart;
	}

	public void setDatestart(Date datestart) {
		this.datestart = datestart;
	}

	public Date getDateend() {
		return dateend;
	}

	public void setDateend(Date dateend) {
		this.dateend = dateend;
	}

	public String getAcceptnames() {
		return acceptnames;
	}

	public void setAcceptnames(String acceptnames) {
		this.acceptnames = acceptnames;
	}

	public String getAccepts() {
		return accepts;
	}

	public void setAccepts(String accepts) {
		this.accepts = accepts;
	}

	public ChartService getChartService() {
		return chartService;
	}

	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}

	public ChartResult getChartResult() {
		return chartResult;
	}

	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

}
